package com.jason.demo.servicehi.concurrent.optional;

import javax.sound.midi.Soundbank;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * @author zhouxinghuai
 * @description description
 * @date 2018-08-16
 */
public class OptionalDemo {

    public static void main(String[] args) {
//        Optional<Integer> optional1 = Optional.ofNullable(null);
//        Optional<Integer> optional2 = Optional.ofNullable(null);
//        System.out.println(optional1 == optional2);// true
//        System.out.println(optional1 == Optional.<Integer>empty());// true
//
//        Object o1 = Optional.<Integer>empty();
//        Object o2 = Optional.<String>empty();
//        System.out.println(o1 == o2);// true
//        System.out.println(o1 == null);// true
//
//        System.out.println("----------");


//        Optional<Integer> optional1 = Optional.ofNullable(1);
//        Optional<Integer> optional2 = Optional.ofNullable(null);
//
//        // isPresent判断值是否存在
//        System.out.println(optional1.isPresent() == true);
//        System.out.println(optional2.isPresent() == false);

        /*Optional<Integer> optional1 = Optional.ofNullable(1);
        Optional<Integer> optional2 = Optional.ofNullable(null);

// 如果不是null,调用Consumer
        optional1.ifPresent((t) ->
                System.out.println("value is " + t)
        );

// null,不调用Consumer
        optional2.ifPresent((t) ->
                System.out.println("value is " + t)
        );*/

//        Optional<Integer> optional1 = Optional.ofNullable(1);
//        Optional<Integer> optional2 = Optional.ofNullable(null);
//
//// orElse
//        System.out.println(optional1.orElse(1000) == 1);// true
//        System.out.println(optional2.orElse(1000) == 1000);// true


//        Optional<Integer> optional1 = Optional.ofNullable(1);
//        Optional<Integer> optional2 = Optional.ofNullable(null);
//
//        System.out.println(optional1.orElseGet(() -> {
//            return 1000;
//        }) == 1);//true
//
//        System.out.println(optional2.orElseGet(() -> {
//            return 1000;
//        }) == 1000);//true


//        Optional<Integer> optional1 = Optional.ofNullable(1);
//        Optional<Integer> optional2 = Optional.ofNullable(null);
//
//        Optional<Integer> filter1 = optional1.filter((a) -> a == null);
//        Optional<Integer> filter2 = optional1.filter((a) -> a == 1);
//        Optional<Integer> filter3 = optional2.filter((a) -> a == null);
//        System.out.println(filter1.isPresent());// false
//        System.out.println(filter2.isPresent());// true
//        System.out.println(filter2.get().intValue() == 1);// true
//        System.out.println(filter3.isPresent());// false


        Optional<Integer> optional1 = Optional.ofNullable(1);
        Optional<Integer> optional2 = Optional.ofNullable(null);

        Optional<String> str1Optional = optional1.map((a) -> "key" + a);
        Optional<String> str2Optional = optional2.map((a) -> "key" + a);

        System.out.println(str1Optional.get());// key1
        System.out.println(str2Optional.isPresent());// false
        System.out.println(str2Optional.get());// key1


    }


}
